package ${config.rootPackage};

import com.gwssi.optimus.core.exception.OptimusException;
import com.gwssi.optimus.core.web.event.OptimusRequest;
import com.gwssi.optimus.core.web.event.OptimusResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/${config.moduleName}/v1/${table.controllerUrl}")
@Api(value = "${table.tableComment!}接口", description = "${table.tableComment!}接口")
public class ${table.controllerPrefix}Controller {

    @Autowired
    private ${table.controllerPrefix}Service ${table.attrPrefix}Service;

    @ApiOperation(value = "新增${table.tableComment!}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "postData", value = "${table.tableComment!}属性", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/saveOrUpdate")
    public void saveOrUpdate(OptimusRequest req, OptimusResponse res) throws OptimusException {
        ${table.boPrefix}BO ${table.attrPrefix}BO = req.getForm("${table.attrPrefix}", ${table.boPrefix}BO.class);
        if (${table.attrPrefix}BO == null) {
            throw new OptimusException("${table.attrPrefix}参数为空，请检查！");
        }
        String id;
        synchronized (${table.attrPrefix}BO.get${table.controllerPrefix}Name().intern()) {
            id = ${table.attrPrefix}Service.saveOrUpdate(${table.attrPrefix}BO);
        }
        res.addAttr("id", id);
    }

    @ApiOperation(value = "根据id删除${table.tableComment!}")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "postData", value = "${table.tableComment!}id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/deleteByKey")
    public void deleteByKey(OptimusRequest req, OptimusResponse res) throws OptimusException {
        String ${table.attrPrefix}Id = (String) req.getAttr("${table.attrPrefix}Id");
        if (StringUtils.isBlank(${table.attrPrefix}Id)) {
            throw new OptimusException("${table.tableComment!}ID为空");
        }
        ${table.attrPrefix}Service.deleteByKey(${table.attrPrefix}Id);
        res.addAttr("success", "success");
    }

    @ApiOperation(value = "根据id查询${table.tableComment!}详情")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "postData", value = "${table.tableComment!}id", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/queryByKey")
    public void queryByKey(OptimusRequest req, OptimusResponse res) throws OptimusException {
        String ${table.attrPrefix}Id = (String) req.getAttr("${table.attrPrefix}Id");
        if (StringUtils.isBlank(${table.attrPrefix}Id)) {
            throw new OptimusException("${table.tableComment!}ID为空");
        }
        ${table.boPrefix}BO ${table.attrPrefix} = ${table.attrPrefix}Service.queryByKey(${table.attrPrefix}Id);
        res.addAttr("${table.attrPrefix}", ${table.attrPrefix});
    }


    @ApiOperation(value = "根据查询条件，查询${table.tableComment!}列表，支持分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "form", name = "postData", value = "${table.tableComment!}筛选条件", required = false, dataType = "String"),
    })
    @RequestMapping(value = "/queryList")
    public void queryList(OptimusRequest req, OptimusResponse res) throws OptimusException {
        String id = (String) req.getAttr("${table.attrPrefix}Id");
        String name = (String) req.getAttr("${table.attrPrefix}Name");
        String label = (String) req.getAttr("${table.attrPrefix}Label");
        List<${table.boPrefix}BO> ${table.attrPrefix}List = ${table.attrPrefix}Service.queryList(id ,name ,label);
        res.addGrid("${table.attrPrefix}List", ${table.attrPrefix}List);
    }

}
